package com.abel.pay.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付服务
 *
 * @author heyangyang
 * @since 2024-04-23
 */
@Service
public class PayService implements InitializingBean, ApplicationContextAware {

    List<IPay> payList;

    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        payList = applicationContext.getBeansOfType(IPay.class).values().stream().toList();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void pay(String code) {
        payList.stream().filter(pay -> pay.support(code)).findFirst().get().handlePay();
    }
}
